package com.itheima.web.controller.system;

import com.github.pagehelper.PageInfo;
import com.itheima.domain.system.Dept;
import com.itheima.domain.system.Module;
import com.itheima.domain.system.Role;
import com.itheima.service.system.DeptService;
import com.itheima.service.system.ModuleService;
import com.itheima.service.system.RoleService;
import com.itheima.web.controller.BaseController;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@RequestMapping(value = "/system/role")
public class RoleController extends BaseController {
    
    @Autowired
    private RoleService roleService;

    @Autowired
    private DeptService deptService;

    @Autowired
    private ModuleService moduleService;

    @RequestMapping(value = "/list", name = "分页查询角色列表")
    @RequiresPermissions("角色管理")
    public String findAll(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "5") Integer size){
        PageInfo<Role> pageInfo = roleService.findByPage(page, size,companyId);
        request.setAttribute("page",pageInfo);
        return "system/role/role-list";
    }

    @RequestMapping(value = "/toUpdate", name = "跳转到角色修改页面")
    public String toUpdate(String id){
        Role role = roleService.findById(id);
        List<Dept> list = deptService.findAll(companyId);
        request.setAttribute("deptList",list);
        request.setAttribute("role", role);
        return "/system/role/role-update";
    }

    @RequestMapping(value = "/toAdd", name = "跳转到角色增加页面")
    public String toAdd(){
        List<Dept> list = deptService.findAll(companyId);
        request.setAttribute("deptList",list);
        return "/system/role/role-add";
    }

    @RequestMapping(value = "/edit", name = "编辑角色信息")
    public String edit(Role role){
        role.setCompanyId(companyId);
        role.setCompanyName(companyName);
        if (StringUtils.isEmpty(role.getId())){
            role.setId(UUID.randomUUID().toString());
            roleService.save(role);
        } else {
            roleService.update(role);
        }
        return "redirect:/system/role/list.do";
    }

    @RequestMapping(value = "/delete", name = "删除角色信息")
    public String delete(String id){
        roleService.delete(id);
        return "redirect:/system/role/list.do";
    }

    @RequestMapping(value = "/roleModule", name = "查询角色模块")
    public String findModuleByRoleId(String roleid) {
        Role role = roleService.findById(roleid);
        request.setAttribute("role", role);

        return "/system/role/role-module";
    }

    @RequestMapping(value = "/initModuleData", name = "初始化模块信息")
    @ResponseBody
    public Object initModuleData(String id){
        List<Module> moduleList = moduleService.findAll(companyId);

        List<Module> userModuleList = moduleService.findByRoleId(id);

        List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
        for (Module module : moduleList) {
            Map<String,Object> map = new HashMap<String, Object>();

            map.put("id", module.getId());
            map.put("pId", module.getParentId());
            map.put("name", module.getName());

            if (userModuleList.contains(module)) {
                map.put("checked", true);
            }

            list.add(map);
        }
        return list;
    }

    @RequestMapping(value = "/updateRoleModule", name = "保存角色模块信息")
    public String updateRoleModule(String roleid, String[] moduleIds) {
        roleService.updateRoleModule(roleid, moduleIds);
        return "redirect: /system/role/list.do";
    }
}
